// 逆波兰表达式求值
// Created by madison on 2022/10/10.
//
#include "vector"
#include "string"
#include "stack"

using namespace std;

class Solution {
public:
    // 方法一：栈
    int evalRPN(vector<string> &tokens) {
        stack<int> stk;
        int n = tokens.size();
        for (int i = 0; i < n; ++i) {
            string &token = tokens[i];
            if (isNumber(token)) {
                stk.push(atoi(token.c_str()));
            } else {
                int num2 = stk.top();
                stk.pop();
                int num1 = stk.top();
                stk.pop();
                switch (token[0]) {
                    case '+':
                        stk.push(num1 + num2);
                        break;
                    case '-':
                        stk.push(num1 - num2);
                        break;
                    case '*':
                        stk.push(num1 * num2);
                        break;
                    case '/':
                        stk.push(num1 / num2);
                        break;
                }
            }
        }
        return stk.top();
    }

    bool isNumber(string &token) {
        return !(token == "+" || token == "-" || token == "*" || token == "/");
    }

    // 方法二：数组模拟栈
    int evalRPN1(vector<string> &tokens) {
        int n = tokens.size();
        vector<int> stk((n + 1) / 2);
        int index = -1;
        for (int i = 0; i < n; ++i) {
            string &token = tokens[i];
            if (token.length() > 1 || isdigit(token[0])) {
                index++;
                stk[index] = atoi(token.c_str());
            } else {
                switch (token[0]) {
                    case '+':
                        index--;
                        stk[index] += stk[index + 1];
                        break;
                    case '-':
                        index--;
                        stk[index] -= stk[index + 1];
                        break;
                    case '*':
                        index--;
                        stk[index] *= stk[index + 1];
                        break;
                    case '/':
                        index--;
                        stk[index] /= stk[index + 1];
                        break;
                }
            }
        }
        return stk[index];
    }
};

int main() {
    Solution solution;
    vector<string> tokens = {"2", "1", "+", "3", "*"};
    printf("%d\n", solution.evalRPN(tokens));
    tokens = {"4", "13", "5", "/", "+"};
    printf("%d\n", solution.evalRPN(tokens));
    tokens = {"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
    printf("%d\n", solution.evalRPN1(tokens));
    return 0;
}